package com.qijian.maindata.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qijian.common.annotation.Log;
import com.qijian.common.core.controller.BaseController;
import com.qijian.common.core.domain.AjaxResult;
import com.qijian.common.core.page.TableDataInfo;
import com.qijian.common.enums.BusinessType;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.qijian.common.utils.poi.ExcelUtil;
import com.qijian.maindata.domain.LineProcessItem;
import com.qijian.maindata.domain.query.LineProcessItemQuery;
import com.qijian.maindata.service.ILineProcessItemService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 物料需求Controller
 *
 * @author qijian
 * @date 2021-11-10
 */
@Api(tags = "工艺管理：物料需求")
@RestController
@RequiredArgsConstructor
@RequestMapping("/maindata/process/item")
public class LineProcessItemController extends BaseController {

    private final ILineProcessItemService lineProcessItemService;

    /**
     * 查询物料需求列表
     */
    @PreAuthorize("@ss.hasPermi('maindata:item:list')")
    @GetMapping("/list")
    public TableDataInfo list(LineProcessItemQuery query, IPage<LineProcessItem> page) {
        return getDataTable(lineProcessItemService.page(Page.of(page.getCurrent(), page.getSize()), WhereEntityTool.invoke(query)));
    }

    /**
     * 查询物料需求成本列表
     */
    @GetMapping("/cost")
    public TableDataInfo cost(LineProcessItemQuery query) {
        return getDataTable(lineProcessItemService.selectProcessItemByBomId(WhereEntityTool.invoke(query)));
    }

    /**
     * 导出物料需求列表
     */
    @PreAuthorize("@ss.hasPermi('maindata:item:export')")
    @Log(title = "物料需求", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(LineProcessItemQuery query, HttpServletResponse response) {
        new ExcelUtil<>(LineProcessItem.class).exportExcel(response, lineProcessItemService.list(WhereEntityTool.invoke(query)), "物料需求数据");
    }

    /**
     * 获取物料需求详细信息
     */
    @PreAuthorize("@ss.hasPermi('maindata:item:query')")
    @GetMapping(value = "/{demandId}")
    public AjaxResult getInfo(@PathVariable("demandId") Long demandId) {
        return AjaxResult.success(lineProcessItemService.getById(demandId));
    }

    /**
     * 新增物料需求
     */
    @PreAuthorize("@ss.hasPermi('maindata:item:add')")
    @Log(title = "物料需求", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LineProcessItem lineProcessItem) {
        return toAjax(lineProcessItemService.save(lineProcessItem));
    }

    /**
     * 修改物料需求
     */
    @PreAuthorize("@ss.hasPermi('maindata:item:edit')")
    @Log(title = "物料需求", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LineProcessItem lineProcessItem) {
        return toAjax(lineProcessItemService.updateById(lineProcessItem));
    }

    /**
     * 删除物料需求
     */
    @PreAuthorize("@ss.hasPermi('maindata:item:remove')")
    @Log(title = "物料需求", businessType = BusinessType.DELETE)
    @DeleteMapping("/{demandIds}")
    public AjaxResult remove(@PathVariable List<Long> demandIds) {
        return toAjax(lineProcessItemService.removeByIds(demandIds));
    }
}
